//
//  CitiesViewController.m
//  eBars
//
//  Created by Vinh Huynh on 4/23/12.
//  Copyright (c) 2012 _eBars GbR_. All rights reserved.
//

#import "CitiesViewController.h"
#import "RegBarsAppDelegate_iPhone.h"
#import "HomeViewController.h"


@implementation CitiesViewController

@synthesize titleView;
@synthesize titleLabel;
@synthesize titleViewBgr;
@synthesize citiesTable;
@synthesize homeViewController;
@synthesize navController;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad{
    [super viewDidLoad];
}

- (void)updateListCities:(NSString*)stateId{
    [DSBezelActivityView newActivityViewForView:self.view withLabel:@"loading..."];
    CityListParser *cityListParser = [[[CityListParser alloc] init] autorelease];
    [cityListParser getCitiesInState:stateId withDelegate:self];
    cityList = [[NSMutableArray alloc] init];
}

#pragma mark -
#pragma mark CityListParser Delegate

- (void)didEndGetCities:(NSArray*)resultDict success:(BOOL) yn{
    if (resultDict) {
        [cityList setArray:resultDict];
    }
    NSLog(@"didEndGetCityList : %@", cityList);
    [citiesTable reloadData];
    [DSBezelActivityView removeViewAnimated:YES];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait || interfaceOrientation == UIInterfaceOrientationPortraitUpsideDown);
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];{
        
    }
}

- (void)dealloc
{
    [titleView release];
    [titleLabel release];
    [titleViewBgr release];
    [citiesTable release];
    [homeViewController release];
    [navController release];
    [super dealloc];
}

#pragma mark - UITableView Delegate & Data Source Methods

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [cityList count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"CellIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier] autorelease];
    }
    NSDictionary *dict = [cityList objectAtIndex:indexPath.row];
    cell.textLabel.text = [dict objectForKey:@"name"];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSDictionary *dict = [cityList objectAtIndex:indexPath.row];
    mainAppDelegate_iPhone.currentCityId = [dict objectForKey:@"id"];
//    self.citiesTable.hidden = YES;
//    [navController.view setFrame:CGRectMake(0, 0, 320, 460)];
//    [UIView beginAnimations:nil context:nil];
//    [UIView setAnimationDuration:1.0f];
//    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromRight
//                           forView:[self view]
//                             cache:YES];
//    [[self view] addSubview:self.presentedViewController.tabBarController.view];        
//    [UIView commitAnimations];
//    [self.view addSubview:navController.view];
//    [self.homeViewController loadRestaurant];
    HomeViewController *controller = [[[HomeViewController alloc] init] autorelease];
    [self.navigationController pushViewController:controller animated:YES];
    [controller loadRestaurant];
}

- (void)flipToHome{
    self.citiesTable.hidden = NO;
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:1.0f];
    [UIView setAnimationTransition:UIViewAnimationTransitionFlipFromLeft
                           forView:[self view]
                             cache:YES]; 
    [navController.view removeFromSuperview];     
    [UIView commitAnimations];
}

@end
